package main

import (
    "fmt"
    "github.com/shirou/gopsutil/cpu"
    "github.com/shirou/gopsutil/disk"
    "github.com/shirou/gopsutil/mem"
    "math"
    "net/http"
    "runtime"
    "time"
    // "os/exec"
)

func IndexHandler(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "text/html; charset=utf-8")
    percent, _:= cpu.Percent(time.Second, false)
    memInfo, _ := mem.VirtualMemory()
    parts, _ := disk.Partitions(false)
    diskInfo, _ := disk.Usage(parts[0].Mountpoint)
    fmt.Fprintln(w, "<h2>电脑信息</h2>")
    fmt.Fprintln(w, "<li>系统类型：",runtime.GOOS,"</li>")
    fmt.Fprintln(w, "<li>系统架构：",runtime.GOARCH,"</li>")
    fmt.Fprintln(w, "<li>CPU核数：",runtime.GOMAXPROCS(0),"</li>")
    fmt.Fprintln(w, "<li>cpu：", "UsedPercent: ",math.Trunc(percent[0]*100+0.5)/100,"%</li>")
    fmt.Fprintln(w, "<li>内存：", "Total: ",memInfo.Total/1000000000, "G, Free:",memInfo.Free/1000000, "MB, UsedPercent:",math.Trunc(memInfo.UsedPercent*100+0.5)/100,"%</li>")
    fmt.Fprintln(w, "<li>硬盘：", "Total: ",diskInfo.Total/1000000000, "G, Free:",diskInfo.Free/1000000000, "G, UsedPercent:",math.Trunc(diskInfo.UsedPercent*100+0.5)/100,"%</li>")
}
/*
用于查看linux系统的系统信息
go mod init server

go run server.go
go build server.go
scp server.out root@106.14.79.80:/root/

compile for linux
SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=amd64
go build -o server.out server.go
 */
func main() {
   http.HandleFunc("/", IndexHandler)
   // Open("http://localhost:8001")
   fmt.Println("running at 127.0.0.1:8001")
   http.ListenAndServe(":8001", nil)
}
// open in browser
// func Open(uri string){
//     cmd := exec.Command("cmd","/c","start", uri)
//     cmd.Start()
// }
